import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// 引入store
import store from '@/store'

// // 引入一级路由组件
// import Home from '@/pages/Home'
// import Login from '@/pages/Login'
// import Register from '@/pages/Register'
// // import Search from '@/pages/Search'
// import Detail from '@/pages/Detail'
// import AddCartSuccess from '@/pages/AddCartSuccess'
// import ShopCart from '@/pages/ShopCart'
// // import Trade from '@/pages/Trade'
// import Pay from '@/pages/Pay'
// import PaySuccess from '@/pages/PaySuccess'
// import Center from '@/pages/Center'

// 引入二级路由组件
// import myOrder from '@/pages/Center/myOrder'
// import groupOrder from '@/pages/Center/groupOrder'

// 备份原来的push
let originPush = VueRouter.prototype.push;
// 重写push
VueRouter.prototype.push = function (location, resolve, reject) {
    // console.log(location, resolve, reject);
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
}
let router = new VueRouter({
    routes: [
        {
            name: 'center',
            path: "/center",
            component: () => import('@/pages/Center'),
            meta: { showFooter: true },
            children: [
                {
                    path: "myorder",
                    component: () => import('@/pages/Center/myOrder'),
                },
                {
                    path: "grouporder",
                    component: () => import('@/pages/Center/myOrder'),
                },
                {
                    path: '/center',
                    redirect: '/center/myorder'
                }
            ]
        },
        {
            name: 'paysuccess',
            path: "/paysuccess",
            component: () => import('@/pages/PaySuccess'),
            meta: { showFooter: true }
        },
        {
            name: 'pay',
            path: "/pay",
            component: () => import('@/pages/Pay'),
            meta: { showFooter: true },
            beforeEnter: (to, from, next) => {
                if (from.path == '/trade') {
                    next()
                } else {
                    next(false)
                }
            }
        },
        {
            name: 'trade',
            path: "/trade",
            component: () => import("@/pages/Trade"),
            meta: { showFooter: true },
            // 独享路由守卫
            beforeEnter: (to, from, next) => {
                if (from.path == '/shopcart') {
                    next()
                } else {
                    next(false)
                }
            }
        },
        {
            name: 'register',
            path: "/register",
            component: () => import('@/pages/Register'),
            meta: { showFooter: true }
        },
        {
            name: 'login',
            path: "/login",
            component: () => import('@/pages/Login'),
            meta: { showFooter: true }
        },
        {
            name: 'shopcart',
            path: "/shopcart",
            component: () => import('@/pages/ShopCart'),
            meta: { showFooter: true }
        },
        {
            name: 'addcartsuccess',
            path: "/addcartsuccess",
            component: () => import('@/pages/AddCartSuccess'),
            meta: { showFooter: true }
        },
        {
            path: "/detail/:detailId?",
            component: () => import('@/pages/Detail'),
            meta: { showFooter: true }
        },
        {
            path: "/home",
            component: () => import('@/pages/Home'),
            meta: { showFooter: true }
        },
        // {
        //     path: "/login",
        //     component: ()=>import('@/pages/Login'),
        //     meta: { showFooter: false }
        // },
        // {
        //     path: "/register",
        //     component: Register,
        //     meta: { showFooter: false }
        // },
        {
            name: 'search',
            path: "/search/:keyword?",
            component: () => import('@/pages/Search'),
            meta: { showFooter: true },
            // props:true
        },
        // 重定向 
        {
            path: '*',
            redirect: "/home"
        },

    ],
    scrollBehavior(to, from, savedPosition) {
        return {
            y: 100
        }
    }
})
router.beforeEach(async (to, from, next) => {
    let token = store.state.user.token
    let name = store.state.user.userData.name

    if (token) {
        if (to.path == '/login') {
            next('/home')
        } else {

            if (name) {
                next()
            }
            else {
                try {
                    await store.dispatch('getUserInfo')
                    next()
                } catch (error) {
                    await store.dispatch('logout')
                    next('/login')
                }
            }
        }
    } else {
        // 未登录不能去“交易、支付、个人中心页面”
        let toPath = to.path
        if (toPath.indexOf('/trade') != -1 || toPath.indexOf('/pay') != -1 || toPath.indexOf('/center') != -1) {
            next('/login?redirect=' + toPath)
        } else {
            next()
        }
    }
})
export default router